//
//  ZCAllSortedViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCAllSortedViewController.h"
#import "ZCAFNetWork.h"
#import "ZCAllSortModel.h"
#import "ZCAllSortTableViewCell.h"
#import <Masonry.h>
@interface ZCAllSortedViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *sortTableView;
@property (nonatomic, strong)NSMutableArray *categaryArr;
@property (nonatomic, strong)NSMutableArray *sceneArr;
@property (nonatomic, strong)NSMutableArray *moodArr;
@property (nonatomic, strong)NSMutableArray *styleArr;
@property (nonatomic, strong)NSMutableArray *topicArr;
@property (nonatomic, strong)NSMutableArray *languageArr;
@end

@implementation ZCAllSortedViewController
#pragma mark 初始化数组
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.categaryArr = [NSMutableArray array];
        self.sceneArr = [NSMutableArray array];
        self.moodArr = [NSMutableArray array];
        self.styleArr = [NSMutableArray array];
        self.topicArr = [NSMutableArray array];
        self.languageArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"F1494A1D-B434-430E-A18A-D21803246AA6"]];
    [self createSortTableView];
    [self createSortTableViewData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(po:) name:@"return" object:nil];
    
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)po:(NSNotification *)not
{
    [self.navigationController popViewControllerAnimated:YES];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:not.userInfo[@"key"],@"n", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil userInfo:dic];
}
#pragma mark 铺设全部页面
- (void)createSortTableView
{
    self.sortTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.sortTableView];
    self.sortTableView.backgroundColor = [UIColor clearColor];
    [self.sortTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    }];
    self.sortTableView.dataSource = self;
    self.sortTableView.delegate = self;
    self.sortTableView.emptyDataSetDelegate = self;
    self.sortTableView.emptyDataSetSource = self;
    
}
#pragma mark 全部页面数据
- (void)createSortTableViewData
{
    [ZCAFNetWork getUrl:@"http://api.dongting.com/song/tag/songlist" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        NSDictionary *dic = result;
        self.categaryArr = [ZCAllSortModel baseModelByArr:dic[@"data"]];
        for (NSDictionary *temp in dic[@"data"][0][@"children"]) {
            ZCAllSortModel *pushModel = [[ZCAllSortModel alloc] initWithDic:temp];
            [self.sceneArr addObject:pushModel];
        }
        for (NSDictionary *temp in dic[@"data"][1][@"children"]) {
            ZCAllSortModel *pushModel = [[ZCAllSortModel alloc] initWithDic:temp];
            [self.moodArr addObject:pushModel];
        }
        for (NSDictionary *temp in dic[@"data"][2][@"children"]) {
            ZCAllSortModel *pushModel = [[ZCAllSortModel alloc] initWithDic:temp];
            [self.styleArr addObject:pushModel];
        }
        for (NSDictionary *temp in dic[@"data"][3][@"children"]) {
            ZCAllSortModel *pushModel = [[ZCAllSortModel alloc] initWithDic:temp];
            [self.topicArr addObject:pushModel];
        }
        for (NSDictionary *temp in dic[@"data"][4][@"children"]) {
            ZCAllSortModel *pushModel = [[ZCAllSortModel alloc] initWithDic:temp];
            [self.languageArr addObject:pushModel];
        }
        [self.sortTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark tableview实现
#pragma mark tableView cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger i = ((ZCAllSortModel *)self.categaryArr[indexPath.section]).children.count;
    if (indexPath.section == 4) {
        return 100;
    } else{
        return 50 * i / 4 ;
    }
}
#pragma mark 分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.categaryArr.count != 0) {
        return self.categaryArr.count;
    }
    return 0;
}
#pragma mark 分区头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    ZCAllSortModel *model = self.categaryArr[section];
    return model.tag;
}
#pragma mark 区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ZCAllSortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[ZCAllSortTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
        cell.backgroundColor = [UIColor clearColor];
        if (indexPath.section == 0) {
            cell.mainArr = self.sceneArr;
            [cell.mainCollectionView reloadData];
        } else if (indexPath.section == 1) {
            cell.mainArr = self.moodArr;
            [cell.mainCollectionView reloadData];
        } else if (indexPath.section == 2) {
            cell.mainArr = self.styleArr;
            [cell.mainCollectionView reloadData];
        } else if (indexPath.section == 3) {
            cell.mainArr = self.topicArr;
            [cell.mainCollectionView reloadData];
        } else if (indexPath.section == 4) {
            cell.mainArr = self.languageArr;
            [cell.mainCollectionView reloadData];

    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
